package com.macgoo.game.dal;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;


public abstract class AbstractDAO<T> {
	private EntityManager em;
	private Class<T> entityClass;
	
	@SuppressWarnings("unchecked")
	public AbstractDAO() {
		em = EMF.getNewEntityManager();
		entityClass =(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}
	
	@SuppressWarnings("unchecked")
	public List<T> QueryAll() {
		try {
			em.getTransaction().begin();
			Query query = em.createQuery("SELECT FROM " + entityClass.getName());
			List list = query.getResultList();
			em.getTransaction().commit();
			return list;
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
		return null;
	}
	
	public void Save(T o) {
		try {
			em.getTransaction().begin();
			em.persist(o);
			em.getTransaction().commit();
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
	}
	public void Update(T o) {
		try {
			em.getTransaction().begin();
			em.merge(o);
			em.getTransaction().commit();
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
	}
	public void Delect(T o) {
		try {
			em.getTransaction().begin();
			if(!em.contains(o))
				o = em.merge(o);
			em.remove(o);
			em.getTransaction().commit();
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
	}
	public void Clear() {
		try {
			em.getTransaction().begin();
			em.clear();
			em.getTransaction().commit();
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
	}
	public void Flush() {
		try {
			em.getTransaction().begin();
			em.flush();
			em.getTransaction().commit();
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
		}
	}
	
	public List<T> FindByProperty(String property, String value) {
		return QueryFiler(property + " = '" + value + "'");
	}
	
	@SuppressWarnings("unchecked")
	public List<T> QueryFiler(String filer) {
		try {
			em.getTransaction().begin();
			String queryStr = "SELECT u FROM " + entityClass.getName() + " u WHERE " + filer;
			Query query = em.createQuery(queryStr);
			List list = query.getResultList();
			em.getTransaction().commit();
			return list;
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
			return null;
		}
	}
	public T Find(Object o) {
		try {
			em.getTransaction().begin();
			T result = em.find(entityClass, o);
			em.getTransaction().commit();
			return result;
		} catch (Exception e) {
			if(em.getTransaction().isActive()) {
				em.getTransaction().rollback();
			}
			return null;
		}
	}
	
	public void Close() {
		em.close();
	}
}
